<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>7.使用解构赋值提高效率</title>
</head>

<body>
  <script>
    //解构就是数组批量赋值给变量的语法糖。
    /*let arr = ['houdunren', 2010];
    let name = arr[0];
    let year = arr[1];*/

    let arr = ['houdunren', 2010]
    let [name, year] = arr;
    console.log(name, year);
    // 可以写到方法中简化赋值操作，也能提高可读性
    function get () {
      return ['houdunren', 2010];
    }
    "use strict";

    let [name2, year2] = get();
    console.log(name2, year2);
    console.log(..."hdcms");
    const [...arr2] = "hdrcms";
    //相当于
    //const [...arr] = ['h','d','c','m','s'];
    console.log(arr2);//['h', 'd', 'r', 'c', 'm', 's']

    const [, year3] = ["后盾人", 2010];
    console.log(year3); //2010

    let [name3, ...args] = ["后盾人", 2010, "houdunren.com"];
    //args展开语法，作为参数吸收后面所有变量
    console.log(name3, args);//后盾人 (2) [2010, 'houdunren.com']
    //作为值类型，会把所有参数打开
    let a = ['cms', ...args];
    console.log(a);//(3) ['cms', 2010, 'houdunren.com']
    //可以设置默认值
    let [name4, year4=2018] = ["后盾人"];
    //args展开语法，作为参数吸收后面所有变量
    console.log(name4, year4);//后盾人 2018
  </script>
</body>

</html>